package com.industrial.system.controller;


import com.industrial.common.core.domain.R;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.system.domain.SysArea;
import com.industrial.system.service.ISysAreaService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *     查询行政区划数据
 * </p>
 * @author SangGuangCan
 * @since 2024-01-16
 */
@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController {

    private ISysAreaService sysAreaService;

    @Autowired
    public SysAreaController(ISysAreaService sysAreaService) {
        this.sysAreaService = sysAreaService;
    }

    @GetMapping("/tree")
    public AjaxResult queryAreaTree(){
        return this.sysAreaService.queryAreaTree();
    }

    @GetMapping("/tree/{parentCode}")
    public AjaxResult queryAreaTreeByParentCode(@PathVariable("parentCode") String parentCode){
        return this.sysAreaService.queryAreaTreeByParentCode(parentCode);
    }

    @GetMapping("/tree/allarea/{parentCode}")
    public AjaxResult queryAreaAllareaTreeByParentCode(@PathVariable("parentCode") String parentCode){
        return this.sysAreaService.queryAreaAllareaTreeByParentCode(parentCode);
    }

    @GetMapping("/queryAreaTreeByCode")
    public  AjaxResult queryAreaTreeByCode(SysArea sysArea){
        return this.sysAreaService.queryAreaTreeByCode(sysArea);
    }

    /**
     * 查询除了街道外的所有行政区
     * @param parentId
     * @return
     */
    @GetMapping("/{parentId}")
    public AjaxResult getAreaByParentId(@PathVariable("parentId") String parentId){
        List<SysArea> areaByParentId = sysAreaService.getAreaByParentId(parentId);
        return AjaxResult.success(areaByParentId);
    }

    /**
     * 查询山东省的地市行政区
     * @param parentId
     * @return
     */
    @GetMapping("/getAreaInfoByOnlyCity/{parentId}")
    public AjaxResult getAreaInfoByOnlyCity(@PathVariable("parentId") String parentId){
        return sysAreaService.getAreaInfoByOnlyCity(parentId);
    }


    @GetMapping("/page")
    public TableDataInfo getAreaByParentIdWithPage(@RequestParam("parentId") String parentId){
        startPage();
        List<SysArea> areaByParentId = sysAreaService.getAreaByParentId(parentId);
        return getDataTable(areaByParentId);
    }

    @GetMapping("/name/{code}")
    public AjaxResult getAreaNameByCode(@PathVariable("code") String code){
        return sysAreaService.getAreaNameByCode(code);
    }

    /**
     * 这个方法最好是用来查询市的，区县可能会存在重复的名称
     * @return
     */
    @GetMapping("/queryAreaCodeByAreaName/{areaName}")
    public R<List<String>> queryAreaCodeByAreaName(@PathVariable("areaName") String areaName){
       List<String> r = sysAreaService.queryAreaCodeByAreaName(areaName);
       return R.ok(r);
    }
}
